/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_real_name.service;

import com.yuecai.modules.yc_real_name.domain.YcRealName;
import com.yuecai.modules.yc_real_name.repository.YcRealNameRepository;
import com.yuecai.modules.yc_real_name.service.dto.YcRealNameDto;
import com.yuecai.modules.yc_real_name.service.dto.YcRealNameQueryCriteria;
import com.yuecai.modules.yc_real_name.service.mapstruct.YcRealNameMapper;
import com.yuecai.modules.yc_user.domain.YcUser;
import com.yuecai.modules.yc_user.repository.YcUserRepository;
import com.yuecai.utils.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-06-23
**/
@Service
@RequiredArgsConstructor
public class YcRealNameService {

    private final YcRealNameRepository ycRealNameRepository;
    private final YcUserRepository ycUserRepository;
    private final YcRealNameMapper ycRealNameMapper;

    
    public Map<String,Object> queryAll(YcRealNameQueryCriteria criteria, Pageable pageable){
        Page<YcRealName> page = ycRealNameRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycRealNameMapper::toDto));
    }

    
    public List<YcRealNameDto> queryAll(YcRealNameQueryCriteria criteria){
        return ycRealNameMapper.toDto(ycRealNameRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public YcRealNameDto findById(Long id) {
        YcRealName ycRealName = ycRealNameRepository.findById(id).orElseGet(YcRealName::new);
        ValidationUtil.isNull(ycRealName.getId(),"YcRealName","id",id);
        return ycRealNameMapper.toDto(ycRealName);
    }



    public YcRealNameDto findByUserId(Long userId) {
        YcRealName ycRealName = ycRealNameRepository.findByUserId(userId);
        return ycRealNameMapper.toDto(ycRealName);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcRealNameDto create(YcRealName resources) {
        Long userId = SecurityUtils.getCurrentUserId();
        resources.setUserId(userId);
        Optional<YcUser> byId = ycUserRepository.findById(userId);
        YcUser ycUser = byId.get();
        ycUser.setRealName(true);
        ycUserRepository.save(ycUser);
        return ycRealNameMapper.toDto(ycRealNameRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcRealName resources) {
        YcRealName ycRealName = ycRealNameRepository.findById(resources.getId()).orElseGet(YcRealName::new);
        ValidationUtil.isNull( ycRealName.getId(),"YcRealName","id",resources.getId());
        ycRealName.copy(resources);
        ycRealNameRepository.save(ycRealName);
    }


    @Transactional
    public void delete() {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Optional<YcUser> byId1 = ycUserRepository.findById(currentUserId);
        YcUser ycUser = byId1.get();
        ycUser.setRealName(false);
        ycUserRepository.save(ycUser);
        ycRealNameRepository.deleteByUserId(currentUserId);
    }

    
    public void download(List<YcRealNameDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcRealNameDto ycRealName : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", ycRealName.getUserId());
            map.put("真实姓名", ycRealName.getRealName());
            map.put("身份证号", ycRealName.getIdentityNumber());
            map.put("营业执照图片 多个用，拼接", ycRealName.getBusinessLicense());
            map.put("店铺地址", ycRealName.getStoreAddress());
            map.put("纬度", ycRealName.getLatitude());
            map.put("经度", ycRealName.getLongitude());
            map.put("创建时间", ycRealName.getCreateTime());
            map.put("修改时间", ycRealName.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}